<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/8/29
 * Time: 21:06
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Form as FormModel;
use think\facade\Request;


class Form extends AdminBase
{
    public function index()
    {
        if(Request::isAjax()){
            $list = FormModel::instance()->order('addtime desc')->select();
            $this->success('数据列表','',$list);
        }else{
            return $this->fetch();
        }
    }

    public function add(){
        if(Request::isPost()){
            $form = Request::param('form/a');
            if( in_array($form['module'],self::$banmodules))
            {
                $this->error('模块名称不能为 '. implode(',',self::$banmodules));
            }
            $form['addtime'] = time();
            $res = FormModel::instance()->add($form);
            if($res === true){
                $this->success('成功',url('index'));
            }else{
                $this->error($res);
            }
        }else{
            return $this->fetch('form');
        }
    }

    public function edit(){
        $formid = Request::param('formid/s');
        $model = FormModel::instance()->getSingle(['guid'=>$formid]);
        if(!$model){
            $this->error('参数错误');
        }
        if(Request::isPost()){
            $form = Request::param('form/a');
            if( in_array($form['module'],self::$banmodules))
            {
                $this->error('模块名称不能为 '. implode(',',self::$banmodules));
            }
            $res = FormModel::instance()->edit($form);
            if($res === true){
                $this->success('成功',url('index'));
            }else{
                $this->error($res);
            }
        }else{
            $this->assign('model',$model);
            return $this->fetch('form');
        }
    }

    public function remove(){
        $guid = Request::param('formid/s');
        $res = FormModel::instance()->remove(['guid'=>$guid]);
        if($res === true){
            $this->success('成功');
        }else{
            $this->error($res);
        }
    }
}